package com.platform.controller;

import com.platform.entity.AccountSnapshotEnergyEntity;
import com.platform.service.AccountSnapshotEnergyService;
import com.platform.service.LotteryRecordService;
import com.platform.service.UserAssetService;
import com.platform.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Configuration
@EnableScheduling
public class EnengyStatisticsTask {
    @Autowired
    private AccountSnapshotEnergyService accountSnapshotEnergyService;
    @Autowired
    private UserAssetService userAssetService;
    @Autowired
    private LotteryRecordService lotteryRecordService;

    /**
     * 定时生成能量值记录
     * 每天晚上1点55
     * @Scheduled(cron = "0 55 1 ? * *")
     */

//    @Scheduled(cron = "0/3 * * * * ?")
//    public void  task(){
//        System.out.println("task  ----->  "+(System.currentTimeMillis()));
//    }

    @Scheduled(cron = "0/1 * * * * ?")
    public void createEnergyStatiscs(){
        System.out.println("能量值统计 ---> 创建定时任务 !");
        try {
            List<Map<String,Object>> queryList = accountSnapshotEnergyService.createEnergyStaticsList();
            AccountSnapshotEnergyEntity accountSnapshotEnergyEntity = new AccountSnapshotEnergyEntity();
            accountSnapshotEnergyEntity.setTotal(userAssetService.queryTotalEnergyTotal());
            accountSnapshotEnergyEntity.setTotalDetail(accountSnapshotEnergyService.queryTotalEnergyDetail());
            accountSnapshotEnergyEntity.setHoldSpc(userAssetService.queryTotalSpcCoin());
            accountSnapshotEnergyEntity.setLotterySpend(lotteryRecordService.queryLotterySendTotal());
            accountSnapshotEnergyEntity.setLotteryPrice(lotteryRecordService.queryLotteryPriceTotal());
            accountSnapshotEnergyEntity.setDate(DateUtils.format(new Date(),"yyyy-MM-dd"));
            accountSnapshotEnergyEntity.setAddTime(new Date());
            for(Map<String,Object> param  : queryList){
                System.out.println(param.get("type").equals(1)+" ------------------------ "+param.get("type"));
                if(param.get("type").equals(19)||param.get("type").equals(20)){
                    accountSnapshotEnergyEntity.setExTotal(accountSnapshotEnergyEntity.getExTotal().add(new BigDecimal(param.get("energy").toString())));
                }else
                if(param.get("type").equals(22)||param.get("type").equals(24)){
                    accountSnapshotEnergyEntity.setBuyTotal(new BigDecimal(param.get("energy").toString()));
                }else
                if(param.get("type").equals(13)||param.get("type").equals(14)||param.get("type").equals(15)||param.get("type").equals(16)||param.get("type").equals(17)||param.get("type").equals(18)){
                    accountSnapshotEnergyEntity.setExtractTotal(accountSnapshotEnergyEntity.getExtractTotal().add(new BigDecimal(param.get("energy").toString())));
                }else
                if(param.get("type").equals(21)){
                    accountSnapshotEnergyEntity.setTaskTotal(new BigDecimal(param.get("energy").toString()));
                }else
                if(param.get("type").equals(25)||param.get("type").equals(26)||param.get("type").equals(27)||param.get("type").equals(37)||param.get("type").equals(28)||param.get("type").equals(38)){
                    accountSnapshotEnergyEntity.setReleaseSpc(accountSnapshotEnergyEntity.getReleaseSpc().add(new BigDecimal(param.get("energy").toString())));
                }else
                if(param.get("type").equals(1)){
                    accountSnapshotEnergyEntity.setExchangeSpc(new BigDecimal(param.get("energy").toString()));
                }
            }
            Integer save = accountSnapshotEnergyService.save(accountSnapshotEnergyEntity);
            if(save>0){
                System.out.println("生成能量统计成功!");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("accountStaticsTask ---> createEnergyStatics : "+e.getMessage());
        }
    }

}
